#include "Customer.hh"
#include "SavingsAccount.hh"
#include "CheckingAccount.hh"
#include "ReportGenerator.hh"
#include "Transaction.hh"

Customer::Customer(const string& name) : name(name) {
    // 自动为客户创建储蓄账户和支票账户
    Account* savings = new SavingsAccount(this);  // 创建储蓄账户
    Account* checking = new CheckingAccount(this);  // 创建支票账户

    addAccount(savings);  // 添加储蓄账户
    addAccount(checking);  // 添加支票账户
}

Customer::~Customer() {
    for (auto acc : accounts) {
        delete acc;  // 释放所有账户的内存
    }
}

void Customer::addAccount(Account* acc) {
    accounts.push_back(acc);  // 将账户添加到账户列表
}

string Customer::getName() const {
    return name;  
}

vector<Account*> Customer::getAccounts() const {
    return accounts;  
}
void Customer::showAccounts() const {
    // 这里可以实现遍历显示所有账户信息的代码
    for (const auto& account : accounts) {
        account->getInfo();  
    }
}

Account* Customer::findAccountByID(const string& id) const {
    // 根据账户ID查找账户
    for (auto acc : accounts) {
        if (acc->getID() == id) {
            return acc;
        }
    }
    return nullptr;  
}

// 根据名字查找客户
Customer* Customer::findCustomerByName(const vector<shared_ptr<Customer>>& customers, const string& name) {
    for (auto customer : customers) {
        if (customer->getName() == name) {
            return customer.get();  
        }
    }
    return nullptr;  
}

void Customer::showAccounts() {
    const auto& accounts = getAccounts();

    cout << "\nThis customer has the following accounts:\n\n";

    for (size_t i = 0; i < accounts.size(); ++i) {
        cout << i + 1 << ". " << accounts[i]->getAccountType()
             << ": " << accounts[i]->getID() << "\n"
             << "   Balance: ¥" << accounts[i]->getBalance() << "\n\n";
    }

    cout << "Options:\n";
    cout << "1. Access Savings Account\n";
    cout << "2. Access Checking Account\n";
    cout << "3. View all transactions for this customer\n";
    cout << "4. Generate customer report\n";
    cout << "5. Return to main menu\n";
    cout << "Please choose: ";

    int choice;
    cin >> choice;
    cin.ignore();

    switch (choice) {
    case 1:
    case 2:
        if (choice >= 1 && choice <= 2 && accounts.size() >= choice) {
            accounts[choice - 1]->showMenu();
        } else {
            cout << "Invalid account selection.\n";
        }
        break;

    case 3: {
        bool hasTransactions = false;

        for (const auto& account : accounts) {
            const auto& txs = account->getTransactions();
            if (txs.empty()) continue;

            cout << "\nTransactions for Account ID: " << account->getID() << "\n";
            for (const auto& t : txs) {
                if (t->getFrom() == account->getID() || t->getTo() == account->getID()) {
                    cout << t->toString() << "\n";
                    hasTransactions = true;
                }
            }
        }

        if (!hasTransactions) {
            cout << "No transactions found for this customer.\n";
        }
        break;
    }

    case 4: {
        ReportGenerator::generateCustomerTransactionReport(shared_from_this());
        break;
    }

    case 5:
        return;

    default:
        cout << "Invalid option.\n";
    }
}
